﻿/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/



class PalindromeList {
public:
    ListNode* findMid(ListNode* head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }
    ListNode* reverse(ListNode* head) {
        ListNode* A = NULL;
        ListNode* B = head;
        ListNode* C = head->next;
        while (B) {
            B->next = A;
            A = B;
            B = C;
            if (C) {
                C = C->next;
            }
        }
        return A;
    }

    bool chkPalindrome(ListNode* A) {
        // write code here
        ListNode* mid = findMid(A);
        ListNode* rightHead = reverse(mid);
        ListNode* left = A;
        ListNode* right = rightHead;
        while (right->next) {
            if (right->val != left->val) {
                return false;
            }
            right = right->next;
            left = left->next;
        }
        return true;
    }
};